import { homeCategoryHeadApi } from '@/api/category'

const state = {
  list: [
    { name: '居家', id: '001' },
    { name: '美食', id: '002' },
    { name: '服饰', id: '003' },
    { name: '母婴', id: '004' },
    { name: '个护', id: '005' },
    { name: '严选', id: '006' },
    { name: '数码', id: '007' },
    { name: '运动', id: '008' },
    { name: '杂项', id: '009' }
  ]
}

const mutations = {
  setList (state, value) {
    state.list = value
  },
  //
  show (state, value) {
    const temp = state.list.find(item => {
      return item.id === value.id
    })
    if (temp) {
      temp.open = true
    }
  },
  hide (state, value) {
    const temp = state.list.find(item => {
      return item.id === value.id
    })
    if (temp) {
      temp.open = false
    }
  }
}

const actions = {
  async getList ({ commit }) {
    const res = await homeCategoryHeadApi()
    res.result.forEach(item => {
      item.open = false
    })
    commit('setList', res.result)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
